package com.zn.controller;

import com.zn.common.constants.Constants;
import com.zn.common.utils.QrCodeUtils;
import com.zn.dao.UserInfoMapper;
import com.zn.dto.HolderDTO;
import com.zn.pojo.VeiReportInfo2;
import com.zn.services.VeiReportInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HTMLController {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private VeiReportInfoService veiReportInfoService;

    @GetMapping({"","/","/login"})
    public String login1(){
        return "login";
    }

    @GetMapping("/qrcode/introduce")
    public String introduce(){
        return "introduce";
    }
    @GetMapping("/user/submit_report")
    public String submit_report(){
        return "user_view/submit_report";
    }
    @GetMapping("/user/submit_report2_2")
    public String submit_report23(){
        return "user_view/submit_report2";
    }
    @GetMapping("/user/submit_report2")
    public String submit_report2(@RequestParam(value = "rid",required = false) Integer rid,Model model){
        if(rid != null){
            VeiReportInfo2 info2 = veiReportInfoService.getVeiReportInfo2ByRid(rid);
            load_image2(info2);
            model.addAttribute("data",info2);
        }
        return "user_view/submit_report2";
    }
    @GetMapping("/user/view_report")
    public String view_report(){
        return "user_view/view_report";
    }
    @GetMapping("/user/view_save_report")
    public String view_save_report(){
        return "user_view/view_save_report";
    }
    @GetMapping("/user/view_save_report2")
    public String view_save_report2(){
        return "user_view/view_save_report2";
    }
    @GetMapping("/user/submit_PDS_report")
    public String submit_PDS_report(){
        return "user_view/submit_PDS_report";
    }
    @GetMapping("/user/submit_SPT_report")
    public String submit_SPT_report(){
        return "user_view/submit_SPT_report";
    }
    @GetMapping("/user/print_report")
    public String user_print_report(){
        return "user_view/print_report";
    }
    @GetMapping("/admin/add_user")
    public ModelAndView add_user(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("admin_view/add_user");
        Integer role = (Integer) request.getSession().getAttribute(request.getSession().getId());
        mav.addObject("role",role);
        return mav;
    }
    //管理员界面
    //审核页面
    @GetMapping("/admin/verify_report")
    public ModelAndView verify_report(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("admin_view/verify_report");
        mav.addObject("holders",getAllHolder());
        Integer role = (Integer) request.getSession().getAttribute(request.getSession().getId());
        mav.addObject("role",role);
        return mav;
    }
    @GetMapping("/admin/verify_report2")
    public ModelAndView verify_report2(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("admin_view/verify_report2");
        mav.addObject("holders",getAllHolder());
        Integer role = (Integer) request.getSession().getAttribute(request.getSession().getId());
        mav.addObject("role",role);
        return mav;
    }
    //审批页面
    @GetMapping("/admin/audit_report")
    public ModelAndView audit_report(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("admin_view/audit_report");
        mav.addObject("holders",getAllHolder());
        Integer role = (Integer) request.getSession().getAttribute(request.getSession().getId());
        mav.addObject("role",role);
        mav.addObject("modify_token",isAuditModifyAuth(role));
        return mav;
    }
    //审批页面2
    @GetMapping("/admin/audit_report2")
    public ModelAndView audit_report2(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("admin_view/audit_report2");
        mav.addObject("holders",getAllHolder());
        Integer role = (Integer) request.getSession().getAttribute(request.getSession().getId());
        mav.addObject("role",role);
        mav.addObject("modify_token",isAuditModifyAuth(role));
        return mav;
    }

    @GetMapping("/admin/archive1")
    public ModelAndView auditArchive1(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("admin_view/archive1");
        mav.addObject("holders",getAllHolder());
        Integer role = (Integer) request.getSession().getAttribute(request.getSession().getId());
        mav.addObject("role",role);
        mav.addObject("modify_token",isAuditModifyAuth(role));
        return mav;

    }
    @GetMapping("/admin/archive2")
    public ModelAndView auditArchive2(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("admin_view/archive2");
        mav.addObject("holders",getAllHolder());
        Integer role = (Integer) request.getSession().getAttribute(request.getSession().getId());
        mav.addObject("role",role);
        mav.addObject("modify_token",isAuditModifyAuth(role));
        return mav;

    }
    @GetMapping("/admin/print_report")
    public ModelAndView admin_print_report(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("admin_view/print_report");
        mav.addObject("holders",getAllHolder());
        Integer role = (Integer) request.getSession().getAttribute(request.getSession().getId());
        mav.addObject("role",role);
        mav.addObject("modify_token",isAuditModifyAuth(role));
        return mav;
    }
    @GetMapping("/admin/print_report2")
    public ModelAndView admin_print_report2(HttpServletRequest request){
        ModelAndView mav = new ModelAndView("admin_view/print_report2");
        mav.addObject("holders",getAllHolder());
        Integer role = (Integer) request.getSession().getAttribute(request.getSession().getId());
        mav.addObject("role",role);
        mav.addObject("modify_token",isAuditModifyAuth(role));
        return mav;
    }

    @GetMapping("qrcode")
    public void qrcode(HttpServletRequest request, HttpServletResponse response) {
//        StringBuffer url = request.getRequestURL();
        // 域名
//        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
        // 再加上请求链接
        String requestUrl = "http://120.53.7.83:8888" + "/login?rid = " + request.getHeader("a");
        try {
            OutputStream os = response.getOutputStream();
            QrCodeUtils.encode(requestUrl, "", os, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/sys/ylInfo")
    public ModelAndView ylQrcode(){
        return new ModelAndView("public/ylqrcode");
    }

    @GetMapping("/user/vehicle_table")
    public String vehicleTable(){
        return "user_view/vehicle_table";
    }

    private List<HolderDTO> getAllHolder(){
        List<HolderDTO> list = new ArrayList<>();
        list.add(new HolderDTO(0,"所有报告"));
        List<HolderDTO> holderDTOS = userInfoMapper.selectHolderByRole(1);
        list.addAll(holderDTOS);
        return list;
    }

    private void load_image2(VeiReportInfo2 reportInfo){
        if(null == reportInfo){
            return;
        }
        if (!StringUtils.isEmpty(reportInfo.getPic1())) {
            reportInfo.setPic1(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic1());
        }
        if (!StringUtils.isEmpty(reportInfo.getPic2())) {
            reportInfo.setPic2(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic2());
        }
        if (!StringUtils.isEmpty(reportInfo.getPic3())) {
            reportInfo.setPic3(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic3());
        }
        if (!StringUtils.isEmpty(reportInfo.getPic4())) {
            reportInfo.setPic4(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic4());
        }
        if (!StringUtils.isEmpty(reportInfo.getPic5())) {
            reportInfo.setPic5(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic5());
        }
        if (!StringUtils.isEmpty(reportInfo.getPic6())) {
            reportInfo.setPic6(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic6());
        }
        if (!StringUtils.isEmpty(reportInfo.getPic7())) {
            reportInfo.setPic7(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic7());
        }
        if (!StringUtils.isEmpty(reportInfo.getPic8())) {
            reportInfo.setPic8(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic8());
        }
        if (!StringUtils.isEmpty(reportInfo.getPic9())) {
            reportInfo.setPic9(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic9());
        }
        if (!StringUtils.isEmpty(reportInfo.getPic10())) {
            reportInfo.setPic10(Constants.REPORT_IMG_VIRTUAL_UTL + reportInfo.getPic10());
        }
    }

    private boolean isAuditModifyAuth(int roleId){
        if(roleId == Constants.ROLE_SYSTEM_BOSS){
            return true;
        }
        return  false;
    }

}
